package com.wsl.tibet_goods.service.impl;

import com.wsl.pojo.Tb_goods;
import com.wsl.pojo.Tb_gouwuche;
import com.wsl.service.Tb_orderService;
import com.wsl.service.Tb_order_itemService;
import com.wsl.tibet_goods.dao.Tb_goodsDao;
import com.wxl.service.Tb_goodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service

public class Tb_goodsServiceimpl implements Tb_goodsService {
    @Autowired
    private Tb_goodsDao tb_goodsDao;
    @Autowired
    private Tb_orderService tb_orderService;
    @Autowired
    private Tb_order_itemService tb_order_itemService;
    @Autowired
    private RedisTemplate<String,Tb_gouwuche> redisTemplate;

    //商品审核
    @Override
    public void CommodityAudit(Long goodsId) {
        Tb_goods tg= tb_goodsDao.selectById(goodsId);
        if(tg!=null && "1".equals(tg.getIs_delete())){
            System.out.println("商品不存在/商品已删除无法上架");
        }
            tg.setIs_marketable("1");//商品上架
            tg.setAudit_status("1");//审核通过
            tb_goodsDao.updateById(tg);
    }
    //商品下架
    @Override
    public void OffTheShelf(Long goodsId) {
        Tb_goods tg= tb_goodsDao.selectById(goodsId);
        if(tg==null && "1".equals(tg.getIs_delete())){
            System.out.println("商品不存在/商品已删除无法上架");
        }
            tg.setIs_marketable("0");//商品下架架
            tb_goodsDao.updateById(tg);
    }
    //商品上架
    @Override
    public void PutOnTheShelf(Long goodsId) {
        Tb_goods tg= tb_goodsDao.selectById(goodsId);
        if(tg==null && "1".equals(tg.getIs_delete())){
            System.out.println("商品不存在/商品已删除无法上架");
        }
            tg.setIs_marketable("1");//商品上架
            tb_goodsDao.updateById(tg);

    }
    //批量上架
    @Override
    public void BatchListing(Long[] goodsId) {
        for(Long item:goodsId){
            Tb_goods tg= tb_goodsDao.selectById(item);
            if(tg==null && "1".equals(tg.getIs_delete())){
                System.out.println("商品不存在/商品已删除无法上架");
            }
                tg.setIs_marketable("1");//商品上架
                tb_goodsDao.updateById(tg);
        }
    }
    //批量下架
    @Override
    public void TheBatchFromTheShelves(Long[] goodsId) {
        for(Long item:goodsId){
            Tb_goods tg= tb_goodsDao.selectById(item);
            if(tg==null && "1".equals(tg.getIs_delete())){
                System.out.println("商品不存在/商品已删除无法上架");
            }
                tg.setIs_marketable("0");//商品上架
                tb_goodsDao.updateById(tg);

        }
    }
    //逻辑删除
    @Override
    public void delete(Long goodsId) {
       Tb_goods tg= tb_goodsDao.selectById(goodsId);
       if("1".equals(tg.getIs_delete())){
           System.out.println("先下架在删除");
       }
           tg.setIs_delete("1");
           tb_goodsDao.updateById(tg);

    }
    //添加商品
    @Override
    public void intergoodds(Tb_gouwuche tb_gouwuche) {
//        tb_goodsDao.insert(tb_goods);
//        Tb_goods tbGoods=new Tb_goods();
//        tbGoods.setGoods_name(tb_goods.getGoods_name());
//        List<Tb_goods> tb_goods1=new ArrayList<>();
//        tb_goods1.add(tb_goods);
//        Map map=new HashMap();
//        map.put("name","大力");
//        map.put("购物车",tb_goods1);
        String name="大力";
        redisTemplate.opsForList().leftPush(name,tb_gouwuche);
    }

    @Override
    public void xiadan() {
      Tb_gouwuche tb_gouwuche = redisTemplate.opsForList().index("大力",0);
        tb_orderService.insert(tb_gouwuche.getTb_order());
        tb_order_itemService.insert(tb_gouwuche.getTb_order_item());
    }
}
